package ie.dcu.spca.model.dao;

import ie.dcu.spca.model.Bean;
import ie.dcu.spca.model.CommonOperation;
import ie.dcu.spca.model.DBconnection;
import ie.dcu.spca.model.Dao;
import ie.dcu.spca.model.bean.ItemsUsers;
import ie.dcu.spca.model.sql.itemsusersSQL;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.database.Cursor;

public class ItemsusersDao extends Dao implements CommonOperation {
    private ItemsUsers cursorToItemusers(Cursor cursor) {
        cursor.moveToNext();
        int idIndex = cursor.getColumnIndex("id");
        int item_idIndex = cursor.getColumnIndex("item_id");
        int user_idIndex = cursor.getColumnIndex("user_id");
        int countIndex = cursor.getColumnIndex("count");
        int creat_atIndex = cursor.getColumnIndex("create_at");
        int update_atIndex = cursor.getColumnIndex("update_at");
        int delete_atIndex = cursor.getColumnIndex("delete_at");
        int id = cursor.getInt(idIndex);
        int item_id = cursor.getInt(item_idIndex);
        String user_id = cursor.getString(user_idIndex);
        int count = cursor.getInt(countIndex);
        long creat_at = cursor.getLong(creat_atIndex);
        long update_at = cursor.getLong(update_atIndex);
        long delete_at = cursor.getLong(delete_atIndex);
        cursor.close();
        return new ItemsUsers(id, item_id, user_id, count, creat_at, update_at,
                delete_at);
    }

    private List<ItemsUsers> cursorToItemusersList(Cursor cursor) {
        List<ItemsUsers> itemusersList = new ArrayList<ItemsUsers>();
        while (cursor.moveToNext()) {
            int idIndex = cursor.getColumnIndex("id");
            int item_idIndex = cursor.getColumnIndex("item_id");
            int user_idIndex = cursor.getColumnIndex("user_id");
            int countIndex = cursor.getColumnIndex("count");
            int creat_atIndex = cursor.getColumnIndex("create_at");
            int update_atIndex = cursor.getColumnIndex("update_at");
            int delete_atIndex = cursor.getColumnIndex("delete_at");
            int id = cursor.getInt(idIndex);
            int item_id = cursor.getInt(item_idIndex);
            String user_id = cursor.getString(user_idIndex);
            int count = cursor.getInt(countIndex);
            long creat_at = cursor.getLong(creat_atIndex);
            long update_at = cursor.getLong(update_atIndex);
            long delete_at = cursor.getLong(delete_atIndex);
            itemusersList.add(new ItemsUsers(id, item_id, user_id, count,
                    creat_at, update_at, delete_at));
        }
        cursor.close();
        return itemusersList;
    }

    public List<ItemsUsers> selectItemusersListByItemid(int id) {
        DBconnection conn = DBconnection.getInstance();
        String sql = String.format(itemsusersSQL.selectItemusersListByItemid,
                id);
        Cursor cursor = conn.querySql(sql);
        return this.cursorToItemusersList(cursor);
    }

    public List<ItemsUsers> selectItemusersListByUserid(int id) {
        DBconnection conn = DBconnection.getInstance();
        String sql = String.format(itemsusersSQL.selectItemusersListByUserid,
                id);
        Cursor cursor = conn.querySql(sql);
        return this.cursorToItemusersList(cursor);
    }

    public int insertItemsusers(int item_id, String user_id, int count) {
        DBconnection conn = DBconnection.getInstance();
        int id = this.selectMaxId() + 1;
        Object[] args = {id, count,
                item_id, user_id, (new Date()).getTime(), 0, 0};
        conn.executeSql(itemsusersSQL.insertItemsusers, args);
        return id;
    }

    @Override
    public Bean selectById(int id) {
        DBconnection conn = DBconnection.getInstance();
        String sql = String.format(itemsusersSQL.selectById, id);
        Cursor cursor = conn.querySql(sql);
        return this.cursorToItemusers(cursor);
    }

    @Override
    public int insertByBean(Bean bean) {
        DBconnection conn = DBconnection.getInstance();
        ItemsUsers itemusers = (ItemsUsers) bean;
        int id = itemusers.getId();
        if (this.existById(id))
            return -1;
        int count = itemusers.getCount();
        int item_id = itemusers.getItem_id();
        String user_id = itemusers.getUser_id();
        long create_at = itemusers.getCreate_at().getTime();
        long update_at = itemusers.getUpdate_at().getTime();
        long delete_at = itemusers.getDelete_at().getTime();
        Object[] args = {id, count,
                item_id, user_id, create_at, update_at, delete_at};
        conn.executeSql(itemsusersSQL.insertByBean, args);
        return id;
    }

    @Override
    public boolean deleteById(int id) {
        DBconnection conn = DBconnection.getInstance();
        if (!this.existById(id))
            return false;
        Object[] args = {(new Date())
                .getTime(), id};
        conn.executeSql(itemsusersSQL.deleteById, args);
        return true;
    }

    @Override
    public boolean deleteByBean(Bean bean) {
        return this.deleteById(((ItemsUsers) bean).getId());
    }

    @Override
    public boolean updateByBean(Bean bean) {
        DBconnection conn = DBconnection.getInstance();
        ItemsUsers itemusers = (ItemsUsers) bean;
        int id = itemusers.getId();
        if (!this.existById(id))
            return false;
        int count = itemusers.getCount();
        int item_id = itemusers.getItem_id();
        String user_id = itemusers.getUser_id();
        long create_at = itemusers.getCreate_at().getTime();
        long update_at = itemusers.getUpdate_at().getTime();
        long delete_at = itemusers.getDelete_at().getTime();
        Object[] args ={count, item_id,
                user_id, create_at, update_at, delete_at, id};
        conn.executeSql(itemsusersSQL.updateByBean, args);
        return true;
    }

    @Override
    public int selectMaxId() {
        DBconnection conn = DBconnection.getInstance();
        String sql = itemsusersSQL.selectMaxId;
        Cursor cursor = conn.querySql(sql);
        int maxid = 0;
        if (cursor.moveToNext()) {
            int idIndex = cursor.getColumnIndex("max");
            maxid = cursor.getInt(idIndex);

        }
        cursor.close();
        return maxid;
    }

    @Override
    public boolean existById(int id) {
        DBconnection conn = DBconnection.getInstance();
        String sql = String.format(itemsusersSQL.existById, id);
        Cursor cursor = conn.querySql(sql);
        boolean f = false;
        if (cursor.moveToNext())
            f = true;
        cursor.close();
        return f;
    }

    @Override
    public boolean isDeletedById(int id) {
        DBconnection conn = DBconnection.getInstance();
        String sql = String.format(itemsusersSQL.isDeletedById, id);
        Cursor cursor = conn.querySql(sql);
        boolean f = false;
        if (cursor.moveToNext())
            f = true;
        cursor.close();
        return f;
    }

    @Override
    public List<Bean> selectAllBean() {
        DBconnection conn = DBconnection.getInstance();
        String sql = itemsusersSQL.selectAllBean;
        Cursor cursor = conn.querySql(sql);
        List<ItemsUsers> itemsuerslist = this.cursorToItemusersList(cursor);
        List<Bean> beanlist = new ArrayList<Bean>();
        for (ItemsUsers b : itemsuerslist) {
            beanlist.add(b);
        }
        cursor.close();
        return beanlist;
    }
}
